package com.pet.petadoption.petstorage.controller;

import com.pet.petadoption.adoption.entity.Adoption;
import com.pet.petadoption.pet.biz.petBizImpl.PetBizImpl;
import com.pet.petadoption.pet.entity.Pet;
import com.pet.petadoption.petstorage.biz.impl.PetstorageBizImpl;
import com.pet.petadoption.petstorage.entity.Petstorage;
import com.pet.petadoption.user.entity.User;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
@RequestMapping("petstorage")
@Log4j2
@CrossOrigin(origins = "http://localhost:8080",maxAge = 36000)
public class PetstorageController {
    @Autowired
    PetstorageBizImpl petstorageBiz;
    @Autowired
    PetBizImpl petBiz;

    @RequestMapping(value = "/getAll")
    @ResponseBody
    public Map<String,Object> getAll(Integer index, Boolean isAdopted) {
//        log.warn(index);
        Integer newIndex = index;
        if (newIndex == null) {
            newIndex = 1;
        }
        int size = 5;
//        log.warn(newIndex);
        List<Petstorage> list = petstorageBiz.findAll(newIndex,size,isAdopted);
        System.out.println(list.toString());
        Map<String, Object> map = new HashMap<>();
        map.put("index", index);
        map.put("list", list);
        return map;
    }


    @RequestMapping(value = "/delById")
    @ResponseBody
    public Boolean delById(Integer id,Integer petId){
        log.warn(petId+"   "+id);

        Boolean delStorage= petstorageBiz.delById(id);
        Integer delPet= petBiz.delPet(petId);
        return delStorage;

    }

    @RequestMapping(value = "/findById")
    @ResponseBody
    public List<Petstorage> findById(Integer id,Boolean isAdopted){

        Petstorage petstorage =petstorageBiz.findById(id,isAdopted);
        List<Petstorage> list = new ArrayList<>();
        if(petstorage==null){
            return list;
        }else{
            list.add(petstorage);
            return list;
        }

    }

    @RequestMapping(value = "/findByUname")
    @ResponseBody
    public List<Petstorage> findByUname (String uname , Boolean isAdopted){
        return petstorageBiz.findByUname(uname,isAdopted);
    }

    @RequestMapping(value = "/delArr")
    @ResponseBody
    public Boolean delArr(String ids ,String petIds){
        log.warn(ids +" "+petIds);
         Integer del=   petstorageBiz.delArr(ids);
         Integer del2 = petBiz.delArr(petIds);

            return (del>0&&del2>0);
    }

    @RequestMapping(value = "/addPetStorage")
    @ResponseBody
    public Boolean addPetStorage(String name, String type, String img, String age, String sex, String info, String userId ,String startDate,String endDate) throws ParseException {
        Pet pet = new Pet();
        pet.setName(name);
        pet.setType(type);
        pet.setAge(Integer.valueOf(age));
        pet.setSex(sex);
        pet.setInfo(info);
        pet.setImg(img);
        petBiz.addPet(pet);
        Pet pet2 = petstorageBiz.getPetId(pet);
        User user = new User();
        user.setId(Integer.valueOf(userId));
        Petstorage petstorage = new Petstorage();
        petstorage.setPet(pet2);
        petstorage.setUser(user);

        SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        petstorage.setStorage_start_date(ft.parse(startDate));
        petstorage.setStorage_end_data(ft.parse(endDate));
        log.warn(petstorage.toString());
        return petstorageBiz.addStorage(petstorage);
    }


    @RequestMapping(value = "/accessPetstorage")
    @ResponseBody
    public boolean accessPetstorage(String id){
        Integer row =petstorageBiz.accessPetStroage(Integer.valueOf(id));
        return row ==1;
    }





}
